package com.wp.design.structural.proxy.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class DynamicProxy implements InvocationHandler {
	private Object target;

	public DynamicProxy(Object target) {
		this.target = target;
	}

	public Object bind(){
		Class c = target.getClass();
		return Proxy.newProxyInstance(c.getClassLoader(),c.getInterfaces(),this);
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object argObject = args[0];
		before();
		Object result = method.invoke(target,argObject);
		after();
		return result;
	}
	private void before(){
		System.out.println("动态代理前置方法");
	}
	private void after(){
		System.out.println("动态代理后置方法");
	}
}
